package com.definesys.smecrailtransit.module.SiteDetails.Fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.SmecManager.SmecAnimation.SmecItemAnimator;
import com.definesys.smecrailtransit.api.SmecRailConstant;
import com.definesys.smecrailtransit.base.SmecBaseFragment;
import com.definesys.smecrailtransit.databinding.FragmentEleRuntimeBinding;
import com.definesys.smecrailtransit.module.SiteDetails.SiteDetailsPresenter;
import com.definesys.smecrailtransit.module.SiteDetails.adapter.SiteDetailsAdapter;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleStationRunTimeBean;
import com.definesys.smecrailtransit.utils.ThreadPoolExecutorUtil;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;

import java.util.ArrayList;
import java.util.HashMap;

import static com.definesys.smecrailtransit.module.SiteDetails.SiteDetailsFragment.SiteSwitch;
import static com.definesys.smecrailtransit.module.SiteDetails.adapter.SiteDetailsAdapter.VIEW_TYPE_RUN_TIME;

/**
 * Created by xupeizuo on 2017/8/21.
 */

public class EleRuntimelengthFragment extends SmecBaseFragment<SiteDetailsPresenter> {

    private FragmentEleRuntimeBinding fragmentEleRuntimeBinding;
    private SiteDetailsAdapter siteDetailsAdapter;
    public static final String GET_SITE_RUN_TIME_SUCCESS="GET_SITE_RUN_TIME_SUCCESS";
    public static final String GET_SITE_RUN_TIME_FAIL="GET_SITE_RUN_TIME_FAIL";
    public int stationId=0;
    public String date="";
    private static SiteDetailsPresenter siteDetailsPresenter;
    private boolean isFirst=true;

    @Override
    protected SiteDetailsPresenter getPersenter() {
        return siteDetailsPresenter;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentEleRuntimeBinding=DataBindingUtil.inflate(inflater, R.layout.fragment_ele_runtime,container,false);
        _initView();
        return fragmentEleRuntimeBinding.getRoot();
    }

    private void _initView(){
        ThreadPoolExecutorUtil.getFixedThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        _initRefresh();
                    }
                });
            }
        });
    }

    private void _initRefresh(){
        siteDetailsAdapter=new SiteDetailsAdapter(getContext(),VIEW_TYPE_RUN_TIME);
        fragmentEleRuntimeBinding.runTimeRecycle.setItemAnimator(new SmecItemAnimator());
        fragmentEleRuntimeBinding.runTimeRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
        fragmentEleRuntimeBinding.runTimeRecycle.setAdapter(siteDetailsAdapter);
        fragmentEleRuntimeBinding.refreshLayout.setHeaderView(SmecRailConstant.getSinaRefreshView());
        fragmentEleRuntimeBinding.refreshLayout.setBottomView(new LoadingView(getContext()));
        fragmentEleRuntimeBinding.refreshLayout.setAutoLoadMore(true);
        fragmentEleRuntimeBinding.refreshLayout.setEnableOverScroll(false);
        fragmentEleRuntimeBinding.refreshLayout.setEnableLoadmore(false);
        fragmentEleRuntimeBinding.refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                return;
            }

            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                getSiteRunTime();
            }
        });
    }

    private void getSiteRunTime(){
        HashMap<String,Object> map=new HashMap<>();
        map.put("stationId",stationId);
        map.put("date",date);
        mPresenter.getSiteRunTime(map);
    }

    public static EleRuntimelengthFragment newInstance(SiteDetailsPresenter presenter) {
        Bundle args = new Bundle();
        EleRuntimelengthFragment fragment = new EleRuntimelengthFragment();
        siteDetailsPresenter=presenter;
        fragment.setArguments(args);
        return fragment;
    }


    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_SITE_RUN_TIME_SUCCESS)
            }
    )
    public void getSiteRunTimeSuccess(ArrayList<EleStationRunTimeBean> arrayList){


        fragmentEleRuntimeBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                fragmentEleRuntimeBinding.refreshLayout.finishRefreshing();
            }
        },200);
        fragmentEleRuntimeBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                siteDetailsAdapter.setRunTimeBeen(arrayList);
            }
        },500);
    }

    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_SITE_RUN_TIME_FAIL)
            }
    )
    public void getSiteRunTimeFail(Object o){
        fragmentEleRuntimeBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                fragmentEleRuntimeBinding.refreshLayout.finishRefreshing();
            }
        },400);
    }

    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(SiteSwitch)
            }
    )
    public void siteSwitch(HashMap<String,Object> hashMap){
        String ways=(String) hashMap.get("way");
        if(ways !=null && ways.equals("1")){
            if(isFirst){
                requestData(hashMap);
            }
        }else {
            requestData(hashMap);
        }
    }

    private void requestData(HashMap<String,Object> hashMap){
        int position=(Integer)hashMap.get("position");
//        if(position ==2){
//            date=hashMap.get("date") ==null ? "":(String) hashMap.get("date");
//            stationId=hashMap.get("stationId") == null ? 0 :(Integer) hashMap.get("stationId");
//            fragmentEleRuntimeBinding.refreshLayout.startRefresh();
//            isFirst=false;
//        }
        date=hashMap.get("date") ==null ? "":(String) hashMap.get("date");
        stationId=hashMap.get("stationId") == null ? 0 :(Integer) hashMap.get("stationId");
        fragmentEleRuntimeBinding.refreshLayout.startRefresh();
        isFirst=false;
    }
}
